<?php

namespace app\supplier\model\page;

use app\common\model\page\Page as PageModel;
use app\proxy\model\proxy\Region;
use app\shop\model\app\App;
use app\proxy\model\auth\User as ProxyUser;
use app\supplier\model\supplier\Supplier;

class Page extends PageModel
{
    public static function getHomeDefault($shop_supplier_id)
    {
        $page = (new static())->where('page_type', '10')
            ->where('is_delete', '0')
            ->where(["shop_supplier_id" => $shop_supplier_id, "is_audit" => 1])
            ->order('is_default desc,page_id desc')->find();
        return $page ?: self::getDefault();
    }

    /**
     * 获取列表
     */
    public function getList($params, $page_type = 20, $shop_supplier_id = 0)
    {
        return $this->where(['is_delete' => 0])
            ->where(['page_type' => $page_type, 'shop_supplier_id' => $shop_supplier_id])
            ->order(['create_time' => 'desc'])
            ->hidden(['page_data'])
            ->paginate($params);
    }

    /**
     * 获取所有自定义页面
     */
    public function getLists($params = [])
    {
        if ($params) {
            return $this->where(['is_delete' => 0])
                ->where($params)
                ->where(['page_type' => 20])
                ->hidden(['page_data'])
                ->order(['create_time' => 'desc'])
                ->select();
        }
        return $this->where(['is_delete' => 0])
            ->where(['page_type' => 20])
            ->hidden(['page_data'])
            ->order(['create_time' => 'desc'])
            ->select();
    }

    /**
     * 新增页面
     */
    public function add($data, $page_type = 20, $shop_supplier_id = 0)
    {

        $supplier = (new Supplier())->find($shop_supplier_id);
        if (!$supplier || $supplier["is_recycle"] == 1 || $supplier["is_audit"] != 1) {
            $this->error = "商户不存在或异常";
            return false;
        }
        $savedata = [
            'page_type' => $page_type,
            'page_name' => $data['page']['params']['name'],
            'page_data' => $data,
            'app_id' => self::$app_id,
            'proxy_id' => $supplier["proxy_id"],
            'province_id' => $supplier["province_id"],
            'city_id' => $supplier["city_id"],
            'shop_supplier_id' => $shop_supplier_id,
        ];
        $noAuth = Supplier::checkauth($shop_supplier_id,'page');
        $savedata['is_audit'] = $noAuth?1:0;
        // 删除app缓存
        App::deleteCache();
        return $this->save($savedata);
    }

    /**
     * 更新页面
     */
    public function edit($data,$shop_supplier_id)
    {
        $noAuth = Supplier::checkauth($shop_supplier_id,'page');
        $savedata = [
            'page_name' => $data['page']['params']['name'],
            'page_data' => $data,
            'is_audit' => $noAuth?1:0,
            'is_default' => 0,
        ];
        // 删除app缓存
        App::deleteCache();
        // 保存数据
        return $this->save($savedata) !== false;
    }

    /**
     * 删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 设置默认页
     */
    public function setDefault()
    {
        if ($this["is_audit"] != 1) {
            $this->error = "未审核或审核不通过不允许设置为默认";
            return false;
        }
        $this->save(['is_default' => 1]);
        $this->where('page_id', '<>', $this['page_id'])->where(["shop_supplier_id" => $this["shop_supplier_id"]])->update(['is_default' => 0]);
        return true;
    }
}